package com.zl.tby.service.impl;

import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyChangeTrendQuery;
import com.zl.tby.model.TbyChangeTrend;
import com.zl.tby.repository.TbyChangeTrendRepo;
import com.zl.tby.service.ITbyChangeTrendService;
import com.zl.tby.vo.TbyChangeTrendForm;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Tran;
import org.noear.wood.IPage;
import org.noear.wood.annotation.Db;
import org.noear.wood.utils.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.List;

/**
 * 物种变化趋势接口实现类
 * @author sunjiaxing
 * @version 1.0
 * @since 2024/8/12 23:23
 */
@Component
public class TbyChangeTrendServiceImpl implements ITbyChangeTrendService {
    
    @Db
    private TbyChangeTrendRepo tbyChangeTrendRepo;

    @Override
    @Tran
    public R<TbyChangeTrend> add(TbyChangeTrendForm form) {
        if (this.tbyChangeTrendRepo.selectCount(mp -> mp.whereTrue().andEq("statistical_time", form.getStatisticalTime()).andEq("species_code", form.getSpeciesCode())) > 0) {
            return R.failed(form.getStatisticalTime()+" 该月份已存在，无法添加");
        }
        TbyChangeTrend entity = form.createEntity();
        entity.setCreateBy(ToolsUtil.getLoginUserId());
        entity.setCreateTime(LocalDateTime.now());
        long id = tbyChangeTrendRepo.insert(entity, true);
        entity.setId(id);
        return R.ok(entity);
    }

    @Override
    @Tran
    public R<TbyChangeTrend> update(TbyChangeTrendForm form) {
        if (this.tbyChangeTrendRepo.selectCount(mp -> mp.whereTrue().andEq("statistical_time", form.getStatisticalTime()).andEq("species_code", form.getSpeciesCode()).andNeq("id",form.getId())) > 0) {
            return R.failed(form.getStatisticalTime()+" 该月份已存在，无法修改");
        }
        TbyChangeTrend entity = this.get(form.getId());
        if (entity == null) {
            return R.failed("数据不存在");
        }
        form.updateEntity(entity);
        entity.setUpdateBy(ToolsUtil.getLoginUserId());
        entity.setUpdateTime(LocalDateTime.now());
        tbyChangeTrendRepo.updateById(entity, false);
        return R.ok(entity);
    }

    @Override
    public R<Void> delete(Long id) {
        // TODO 验证是否被引用
        int flag = tbyChangeTrendRepo.deleteById(id);
        return flag > 0 ? R.ok() : R.failed("删除失败");
    }

    @Override
    public TbyChangeTrend get(Long id) {
        return tbyChangeTrendRepo.selectById(id);
    }

    @Override
    public TbyPageResult<TbyChangeTrend> page(TbyChangeTrendQuery query) {

        IPage<TbyChangeTrend> iPage = tbyChangeTrendRepo.selectPage(query.getOffset(), query.getSize(), mp -> {
            mp.whereTrue()
                    .andIf(StringUtils.isNotEmpty(query.getSpeciesCode()), "species_code = ?", query.getSpeciesCode());
            if(query.getStatisticalTime()!=null){
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(query.getStatisticalTime());
                calendar.add(Calendar.MONTH, 1);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                String formattedDate = sdf.format(calendar.getTime());
                mp.andIf(StringUtils.isNotEmpty(formattedDate), "statistical_time = ?", formattedDate);
            }
            if (StringUtils.isNotEmpty(query.getOrder())) {
                mp.orderBy(query.getOrder());
            }
        });
        return new TbyPageResult(iPage, query);
    }

    @Override
    public List<TbyChangeTrend> list(TbyChangeTrendQuery query) {
        return tbyChangeTrendRepo.selectList(mapperWhereQ -> {
            mapperWhereQ.whereTrue().andIf(StringUtils.isNotEmpty(query.getSpeciesCode()), "species_code = ?", query.getSpeciesCode())
                    .andIf(query.getStatisticalTime()!=null, "statistical_time = ?", query.getStatisticalTime());
            if (StringUtils.isNotEmpty(query.getOrder())) {
                mapperWhereQ.orderBy(query.getOrder());
            }
        });
    }
}
